package com.qiongyou.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;

import com.nineoldandroids.view.ViewHelper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private DrawerLayout layout_drawer;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvents();
    }


    //初始化控件
    public void initView() {
        //获取DrawerLayout对象
        layout_drawer = ((DrawerLayout) findViewById(R.id.layout_drawer));
        //设置DrawerLayout对象的打开方式
        layout_drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
        fragments=new ArrayList<Fragment>();
        fragments.add(new CommunityFragment());
    }

    //初始化事件
    public void initEvents() {
        //DrawerLayout的监听事件
        layout_drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
            /**
             * 抽屉改变时调用
             * @param drawerView    抽屉布局View
             * @param slideOffset   抽屉滑动偏移量
             */
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //从DrawerLayout中获取主布局
                View view_main = layout_drawer.getChildAt(0);
                //记录当前改变的抽屉
                View currentView = drawerView;
                //获取动画效果的比例值(例如缩放，平移等)
                float scale = 1 - slideOffset;

                float rightScale = 0.8f + scale * 0.2f;
                //根据抽屉布局的tag值来确定加载相应的布局
                if (drawerView.getTag().equals("LEFT")) {
                    float leftScale = 1 - 0.3f * scale;
                    //设置左侧抽屉的效果
                    ViewHelper.setScaleX(currentView, leftScale);
                    ViewHelper.setScaleY(currentView, leftScale);
                    ViewHelper.setAlpha(currentView, 0.6f + 0.4f * (1 - scale));
                    ViewHelper.setTranslationX(view_main,currentView.getMeasuredWidth() * (1 - scale));
                    ViewHelper.setPivotX(view_main, 0);
                    ViewHelper.setPivotY(view_main,view_main.getMeasuredHeight() / 2);
                    //设置主布局的效果
                    ViewHelper.setScaleX(view_main, rightScale);
                    ViewHelper.setScaleY(view_main, rightScale);
                    //Invalidate方法实现界面刷新
                    view_main.invalidate();
                } else {
                    //不是左侧抽屉的直接设置主布局
                    ViewHelper.setTranslationX(view_main,-currentView.getMeasuredWidth() * slideOffset);
                    ViewHelper.setPivotX(view_main, view_main.getMeasuredWidth());
                    ViewHelper.setPivotY(view_main,view_main.getMeasuredHeight() / 2);
                    ViewHelper.setScaleX(view_main, rightScale);
                    ViewHelper.setScaleY(view_main, rightScale);
                    //Invalidate方法实现界面刷新
                    view_main.invalidate();
                }
            }

            //打开抽屉
            @Override
            public void onDrawerOpened(View drawerView) {

            }

            //关闭抽屉
            @Override
            public void onDrawerClosed(View drawerView) {
                layout_drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
            }

            //改变抽屉的状态
            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    public void imageButtonClick(View v){
        switch (v.getId()){
            case R.id.imageButton_user:
                break;
            //点击右侧进入右侧抽屉
            case R.id.imageButton_search:
                //打开右侧布局
                layout_drawer.openDrawer(Gravity.RIGHT);
                //设置DrawerLayout对象的打开方式
                layout_drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.RIGHT);
                break;
        }
    }

}
